package org.wz.complex.work.conf;

import cn.hutool.core.lang.Dict;
import jakarta.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.wz.complex.work.interceptor.UserLoginInterceptor;

@Configuration
public class WebConf implements WebMvcConfigurer {

    @Resource
    private UserLoginInterceptor userLoginInterceptor;

    @Value("${upload.file}")
    private String uploadFile;

    @Value("${upload.tmp}")
    private String uploadTmp;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");
        //
        Dict set = Dict.create()
                .set("file", uploadFile)
                .set("tmp", uploadTmp)
                ;
        //
        set.forEach((k, v) -> {
            registry.addResourceHandler("/" + k.trim() + "/**")
                    .addResourceLocations("file:" + v.toString().trim());
        });
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //
        String[] excludes = {
                "",
                "/",
                "/html/**",
                "/err/**",
                "/static/**",
                "/login/**",
                "/file/**",
                "/tmp/**"
        };
        // 注册拦截器
        registry.addInterceptor(userLoginInterceptor)
                // 拦截所有路径
                .addPathPatterns("/**")
                // 排除不需要拦截的路径
                .excludePathPatterns(excludes);
    }



}